*File to join game and user data with NOAA weather data


***inputs:
* $Data/user_activeisd_us_stations.dta
* $Data/GameUser_zd.dta
* $Data/DailyWeather_ISD`y' y =2013:2018
* $Data/DailyWeather_NEWISD`y'.dta  y =2013:2018
* $Data/DailyPrecipISD`y'.dta  y =2013:2018

***outputs:
* $Data/GameUserweighted_ISDweather_zd.dta



*** 1. GET STATION LOCATIONS FOR EACH HOUR THAT WE NEED DATA FOR***

* tell us which monitors to match users to 
   use $Data/user_activeisd_us_stations_Aug2019.dta, clear

  
     
   * tells us which hours and locations and monitors to get data for 
   joinby zip using $Data/GameUser_zd.dta

   keep zip date WBAN USAF lat longi LAT LON STATION *dist*

***2. MERGE WEATHER DATA FOR EACH HOUR AND LOCATION AND STATION***


	forval y = 2013(1)2018 {
		joinby  date WBAN USAF using $Data/DailyWeather_ISD`y'.dta, unmatched(master) update
		drop _merge
	}
	
	
destring USAF, ignore("A") replace
 
	forval y = 2013(1)2018 {
		joinby  date WBAN USAF using $Data/DailyWeather_NEWISD`y'.dta, unmatched(master) update
		drop _merge
	}
	
	 
	*drop if temperature ==.
	
	
	* precipitation data
	
	forval y = 2013(1)2018 {
		joinby  date WBAN USAF using $Data/DailyPrecipISD`y'.dta, unmatched(master) update
		drop _merge
	}
	
	
	gen precipitation = precip_measurement > 0 
	
	


***3. GET CLOSEST WIND DIRECTION, CALCULATE THE WEIGHTED AVERAGE OF WEATHER VALUE BY ELEMENTTYPE AND ZIPCODE AND DATE AND HOUR***

	by zip date (isd_dist), sort: gen closest_isdwind = _n==1 if median_wind_direction <.
	gen closest_calm = calm if closest_isdwind==1
	gen median_closestwinddir = median_wind_direction if closest_isdwind == 1 
	
		
	gen weight = 1/isd_dist
	by zip date, sort: egen av_isd_dist =mean(isd_dist) 

	foreach var in temperature wind_speed  calm precipitation {

		gen `var'_distance=`var'*weight

		by zip date, sort: egen sum_`var' = total(`var'_distance)
		by zip date, sort: egen sum_weight_`var' =  total(weight) if `var'_distance<.

		gen weighted_`var' =  sum_`var'/sum_weight_`var'
		

		}

	
	
	
	keep zip date weighted_*  av_isd_dist closest* median_closestwinddir



	keep if  weighted_temp <.  & ( median_closestwinddir <.)  // lose about half with aq 20 miles, isd at 30 miles
	
	
	replace weighted_temp = weighted_temp/10 // ISD temperature is scaled by 10
	
save $Data/GameUserweighted_ISDweather_zd.dta , replace





